using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Collections;

using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;
using Camalon.Drawing;
using System.ComponentModel.Design.Serialization;
using System.CodeDom;

using System.Reflection;

namespace Camalon.Drawing.Design
{
	public interface ISupportAdornment
	{
		ShapeAdornment Adornment
		{
			get;
		}

		ShapeAdornment CreateAdornment(ISelectionService selectionService);
	}

	public interface IControlPointDesigner
	{
		void BeginEditControlPoints(  );
		void EndEditControlPoints(  );
	}

	/// <summary>
	/// Summary description for ShapeDesigner.
	/// </summary>
	public class ShapeDesigner:ComponentBaseDesigner, ISupportAdornment,IControlPointDesigner
	{

		public Shape Shape
		{
			get{ return this.Component as Shape;}

		}

		protected override void Dispose(bool disposing)
		{
			if( disposing)
			{
				if( this.ChangeService != null)
				{
					this.ChangeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged);
					this.ChangeService.ComponentRemoved -=new ComponentEventHandler(OnComponentRemoved);
				}
				if( this.DesignerHost != null)
					this.DesignerHost.LoadComplete-=new EventHandler(DesignerHost_LoadComplete);
			}



//			if(_mcsHelper != null) 
//			{
//				_mcsHelper.Dispose();
//				_mcsHelper = null;
//			}



			base.Dispose (disposing);
		}

		#region ISupportAdornment
		private ShapeAdornment _adornment = null;
		[Browsable(false), DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		public ShapeAdornment Adornment
		{
			get{ return _adornment;}
		}

		public virtual ShapeAdornment CreateAdornment(/*Shape shape,*/ISelectionService selectionService)
		{
			return new ShapeAdornment( (Shape)this.Component, selectionService );
		}

		#endregion ISupportAdornment


		public virtual bool ShouldCreateRendererControl()
		{
			return true;
		}


		private bool _shapeStatusChanging;
		private void RefreshShapeStatus()
		{
			if( _shapeStatusChanging ) return;
			try
			{
				_shapeStatusChanging = true;
				Shape shape = this.Shape;

				if( shape == null) return;

				IDesignerHost host = this.DesignerHost;
				if( host == null) return;

				//Reset status
				//----------------------------

				int status = 0;

				if( shape is ShapeContainer) status|= 2;

				
				if(  this.Inherited ) status|=1;
				else
				{
					PropertyDescriptor propMod = TypeDescriptor.GetProperties(shape)["Modifiers"];
					if( propMod != null)
					{
						MemberAttributes ma = (MemberAttributes)propMod.GetValue( shape );
						if( ma != MemberAttributes.Private && ma != MemberAttributes.Assembly  ) 
							status|=1;
					}
					else FileWatcher.WriteLine("Modifiers propery not found");
				}



				if( status != shape.Status )
				{
					DesignerTransaction tx = null;

					if( !host.InTransaction ) tx = host.CreateTransaction("Do not undo this operation");

					using (tx)
					{
						PropertyDescriptor prop = TypeDescriptor.GetProperties(shape)["Status"];
						prop.SetValue(shape,status);// Set status
						if( tx != null) tx.Commit();
					}
				}
				
			}
			catch( Exception e )
			{
				FileWatcher.WriteLine( "ShapeDesigner:RefreshShapeStatus failed..! " + e.Message);
				FileWatcher.WriteLine( e.StackTrace);

			}
			finally
			{
				_shapeStatusChanging = false;
			}
		}



		#region OnSetComponentDefaults


		static PropertyDescriptorCollection pdcControl = TypeDescriptor.GetProperties(typeof(ShapeControl));
		static PropertyDescriptorCollection pdcShape = TypeDescriptor.GetProperties(typeof(Shape));

        public override void InitializeNewComponent(IDictionary defaultValues)
        {
            //Add a renderer (ShapeControl) for this Shape
            //------------------------------------------
            if (!this.ShouldCreateRendererControl()) return;// || this.Inherited) return;

            ContainerControl root = this.RootComponent as ContainerControl;

            if (root == null) return;

            int dxy = 50;
            Point shapePosition = new Point(dxy, dxy);

            Point controlPosition = Control.MousePosition;
            controlPosition = root.PointToClient(controlPosition);
            if (controlPosition.X < 0) controlPosition.X = 0;
            if (controlPosition.Y < 0) controlPosition.Y = 0;

            Size controlSize = this.Shape.Size;
            controlSize.Width += 2 * dxy;
            controlSize.Height += 2 * dxy;

            Rectangle controlBounds = new Rectangle(controlPosition, controlSize);

            IDesignerHost host = this.DesignerHost;
            host = host.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (host == null) return;

            bool addToControls = true;
            DesignerTransaction tx = this.DesignerHost.CreateTransaction("Creating Shape control");
            if (tx == null) return;

            try
            {
                //INameCreationService ncs = host.GetService( typeof(INameCreationService)) as INameCreationService;
                //string name = TypeDescriptor.GetComponentName( this.Shape) + "Control" ;
                //if(! ncs.IsValidName( name )) {tx.Cancel(); return;}
                //ShapeControl sc =  host.CreateComponent( typeof(ShapeControl)), TypeDescriptor.GetComponentName( this.Shape) + "Control" ) as ShapeControl;

                ShapeControl sc = null;
                ToolboxItem tbiSc = new ToolboxItem(typeof(ShapeControl));

                //-----------------------------------------------------------------------------
                ParentControlDesigner pcd = this.DesignerHost.GetDesigner(this.DesignerHost.RootComponent) as ParentControlDesigner;
                if (pcd != null)
                {
                    MethodInfo mi = typeof(ParentControlDesigner).GetMethod("CreateTool",
                        BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null,
                        new Type[] { typeof(ToolboxItem), typeof(Rectangle) }, null);

                    if (mi != null)
                    {
                        object[] args = new object[] { tbiSc, root.RectangleToScreen(controlBounds) };


                        mi.Invoke(
                            pcd,
                            BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, args, null);

                        sc = this.SelectionService.PrimarySelection as ShapeControl;

                        addToControls = false;
                    }

                }
                //-----------------------------------------------------------------------------
                //					IToolboxUser tbu = this.DesignerHost.GetDesigner( this.DesignerHost.RootComponent) as IToolboxUser;
                //					if( tbu != null)
                //					{
                //						tbu.ToolPicked( tbiSc );
                //						sc = this.SelectionService.PrimarySelection as ShapeControl;
                //					}
                //-----------------------------------------------------------------------------
                if (sc == null)
                {
                    IComponent[] csc = tbiSc.CreateComponents(this.DesignerHost);//This is the Better way to do it in this override					
                    if (csc != null && csc.Length > 0) sc = csc[0] as ShapeControl;
                }

                if (sc == null) sc = host.CreateComponent(typeof(ShapeControl)) as ShapeControl;
                if (sc == null) { if (tx != null) tx.Cancel(); return; }

                PropertyDescriptor propScShape = pdcControl["Shape"];
                PropertyDescriptor propScLocation = pdcControl["Location"];
                PropertyDescriptor propScSize = pdcControl["Size"];
                PropertyDescriptor propThisPosition = pdcShape["Position"];
                if (this.Shape.Position != shapePosition) propThisPosition.SetValue(this.Shape, shapePosition);
                if (sc.Location != controlPosition) propScLocation.SetValue(sc, controlPosition);
                if (sc.Size != controlSize) propScSize.SetValue(sc, controlSize);
                if (sc.Shape != this.Shape) propScShape.SetValue(sc, this.Shape);


                //					if(!( this.Shape is ShapeContainer ) && this.Shape.Pen == null)
                //					{
                //						ToolboxItem tbiPen = new ToolboxItem(typeof( ShapePen ));
                //						IComponent[] cPen = tbiPen.CreateComponents(this.DesignerHost);//This is the Better way to do it in this override					
                //						ShapePen pen = null;
                //
                //						if( cPen != null && cPen.Length > 0) pen = cPen[0] as ShapePen;	
                //						if( pen == null) pen = host.CreateComponent( typeof(ShapePen)) as ShapePen;
                //
                //						PropertyDescriptor propThisPen = pdcShape["Pen"];
                //						propThisPen.SetValue( this.Shape, pen);
                //					}


                if (addToControls && !root.Controls.Contains(sc)) root.Controls.Add(sc);

                //					sc.BringToFront();

                if (tx != null) tx.Commit();

            }
            catch (Exception e)
            {
                FileWatcher.WriteLine("ShapeDesigner::OnSetComponentDefaults (Tx)... " + e.Message + "\n" + e.StackTrace);
                if (tx != null) tx.Cancel();
            }
            finally
            {
                base.InitializeNewComponent(defaultValues);

            }


            base.InitializeNewComponent(defaultValues);
        }
        

		#endregion OnSetComponentDefaults

		#region CreateMenuCommands

//		MenuCommandsHelper _mcsHelper;


		#endregion 



		public override void Initialize(System.ComponentModel.IComponent component)
		{
			try
			{
				base.Initialize (component);


				Shape shape = component as Shape;
			
				if( shape == null) return;

				_adornment = this.CreateAdornment( this.SelectionService );

				if( this.ChangeService != null)
					this.ChangeService.ComponentChanged+=new ComponentChangedEventHandler(OnComponentChanged);


				this.DesignerHost.LoadComplete+=new EventHandler(DesignerHost_LoadComplete);
				this.ChangeService.ComponentRemoved+=new ComponentEventHandler(OnComponentRemoved);
				this.SelectionService.SelectionChanged+=new EventHandler(OnSelectionChanged);
			

//				if( _mcsHelper == null)
//				{
//					_mcsHelper = new MenuCommandsHelper( this.MenuCommandService, GetUsedCommandIDs());// CreateMenuCommands( this.MenuCommandService );
//					_mcsHelper.AlignToGrid += new EventHandler( OnAlignToGrid );
//				}

			}
			catch( Exception e)
			{
				FileWatcher.WriteLine("Shape Initalize failed:" + e.Message);
			}
		}
		
		private void OnAlignToGrid(object sender, EventArgs e)
		{
			FileWatcher.WriteLine("Align to grid called:" + TypeDescriptor.GetComponentName( this.Shape ));
		}

		private bool IsReferenced
		{
			get
			{
				if( this.ReferenceService == null) return false;

				object[] c =  this.ReferenceService.GetReferences( typeof( IShapeProvider ));
				if( c == null || c.Length == 0) return false;

				foreach( IShapeProvider sp in c ) if( sp.Shape == this.Shape) return true;

				return false;


			}
		}

		private void OnSelectionChanged(object sender, EventArgs e)
		{
			try
			{

				if( this.DesignerHost.Loading) return;

//				if( _mcsHelper != null &&
//					this.RootDesigner == null && 
//					this.IsReferenced && 
//					this.SelectionService.PrimarySelection == this.Shape &&
//					this.SelectionService.SelectionCount == 1 )
//					_mcsHelper.Add();


				UpdateVerbs();
			}
			catch(InvalidOperationException)
			{
				FileWatcher.WriteLine( "ShapeDesigner::OnSelectionChanged AddCommand failed");
			}
		}



		[DesignOnly(true)]
		[ReadOnly(true)]
		[Category("Layout")]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual AnchorStyles Anchor
		{
			get
			{
				Shape shape = this.Component as Shape;
				if( shape == null) return AnchorStyles.None;
				return shape.Anchor;
			}
		}
		

		[DesignOnly(true)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[ReadOnly(true)]
		[Category("Layout")]
		public virtual string PathBounds
		{
			get
			{ 
				Shape shape = this.Component as Shape;
				Rectangle bounds = Rectangle.Round( shape.GetPathBounds());
				return string.Format("X={0} ; Y={1} ; Width={2} ; Height={3}", 
					bounds.X,bounds.Y, bounds.Width, bounds.Height);  
			}
		}


		[DesignOnly(true)]
		[ReadOnly(true)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[Category("Transformation")]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual Scale Scale
		{
			get{ return Scale.Empty;}
		}

		[DesignOnly(true)]
		[ReadOnly(true)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[Category("Transformation")]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual Warp Warp
		{
			get{ return Warp.Empty;}
		}

		[DesignOnly(true)]
		[ReadOnly(true)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[Category("Transformation")]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual int Rotation
		{
			get{ return 0;}
		}

		[DesignOnly(true)]
		[ReadOnly(true)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[Category("Transformation")]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual Shear Shear
		{
			get{ return Shear.Empty;}
		}

		[DesignOnly(true)]
		[ReadOnly(true)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[Category("Layout")]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual Size Size
		{
			get
			{ 
				Shape shape = this.Component as Shape;
				if( shape == null) return Size.Empty;
				return shape.Size;
			}
		}

		[DesignOnly(true)]
		[ReadOnly(true)]
		[Category("Style")]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		[EditorBrowsable( EditorBrowsableState.Never )]
		public virtual Outline Outline
		{
			get
			{
				return null;
			}
		}



		protected override void PreFilterProperties(System.Collections.IDictionary properties)
		{
			Shape shape = this.Component as Shape;

			if( shape == null) return;

			
			base.PreFilterProperties (properties);

//			if( !shape.CanScale )
//				properties["Scale"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Scale", 
//					typeof(Scale));
//
//			if( !shape.CanShear )
//				properties["Shear"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Shear", 
//					typeof(Shear));
//			
//			if( !shape.CanWarp )
//				properties["Warp"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Warp", 
//					typeof(Warp));
//
//			if( !shape.CanRotate )
//				properties["Rotation"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Rotation", 
//					typeof(int));
//
//			if( !shape.CanResize )
//				properties["Size"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Size", 
//					typeof(Size));
//
//			if( !shape.CanOutline )
//				properties["Outline"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Outline", 
//					typeof(Outline));
//
//			if( !shape.CanAnchor )
//				properties["Anchor"] = TypeDescriptor.CreateProperty(
//					typeof(ShapeDesigner), 
//					"Anchor", 
//					typeof(AnchorStyles));

		}
	


		public virtual void BeginEditControlPoints(  )
		{

		
		}

		public virtual void EndEditControlPoints(  )
		{
						
		}

		private void DesignerHost_LoadComplete(object sender, EventArgs e)
		{
			RefreshShapeStatus();

		}



		public override ICollection AssociatedComponents
		{
			get
			{
				
				Shape shape = (Shape)this.Component;
				ArrayList list = new ArrayList();
				
				if(shape.Pen != null) list.Add( shape.Pen );
				if(shape.Fill != null) list.Add( shape.Fill );
				if(shape.Shade != null) list.Add( shape.Shade );
				if(shape.TextPen != null) list.Add( shape.TextPen);
				if(shape.TextFill != null) list.Add(shape.TextFill);
				if(shape.TextShade != null) list.Add(shape.TextShade);
				if(shape.Outline != null) list.Add(shape.Outline);

				return list;
				
				
			}
		}


		private void OnComponentRemoved(object sender, ComponentEventArgs e)
		{
			if( e.Component == null ) return;

			
			if( e.Component == this.Shape.Pen ) pdcShape["Pen"].SetValue( this.Shape, null);
			if( e.Component == this.Shape.Fill ) pdcShape["Fill"].SetValue( this.Shape, null);
			if( e.Component == this.Shape.Shade ) pdcShape["Shade"].SetValue( this.Shape, null);
			if( e.Component == this.Shape.TextPen ) pdcShape["TextPen"].SetValue( this.Shape, null);
			if( e.Component == this.Shape.TextFill ) pdcShape["TextFill"].SetValue( this.Shape, null);
			if( e.Component == this.Shape.TextShade ) pdcShape["TextShade"].SetValue( this.Shape, null);
			if( e.Component == this.Shape.Outline ) pdcShape["Outline"].SetValue( this.Shape, null);

		}

		private void OnComponentChanged(object sender, ComponentChangedEventArgs e)
		{

			if(e.Member == null ) return;


			if(	
				e.Member.Name == "Warp" || 
				e.Member.Name == "Rotation" ||
				e.Member.Name == "Scale" ||
				e.Member.Name == "Shear") 
				UpdateVerbs();


			if( this.Shape != null) RefreshShapeStatus();

	}


		DesignerVerb			_verbWarpToCenter	= null;
		DesignerVerb			_verbCenterToWarp	= null;	
		DesignerVerb			_verbSaveAs			= null;
		DesignerVerb			_verbResetTransform	= null;

		DesignerVerbCollection	_verbs				= null;


		public override DesignerVerbCollection Verbs
		{
			get
			{
				if( !(this.Component is Shape)) return null;
				Shape shape = this.Component as Shape;

				//If you want to toggle Verb box in the prop.window
				//if( CenterOffset.IsEmpty) return null;

				DesignerVerbCollection baseVerbs = base.Verbs;
//				if( base.Verbs.Count != 0)
//					FileWatcher.WriteLine("Base contains verbs");
				
				if( _verbs == null ) 
				{
					_verbs = new DesignerVerbCollection();
					_verbSaveAs			= new DesignerVerb("Save As..", new EventHandler(OnSaveAs));
					_verbResetTransform	= new DesignerVerb("Reset Transformations", new EventHandler(OnResetTransform));

					_verbWarpToCenter	= new DesignerVerb("Warp to center", new EventHandler(OnWarpToCenter));
					_verbCenterToWarp	= new DesignerVerb("Center to warp", new EventHandler(OnCenterToWarp));


					_verbs.Add( _verbSaveAs );
					_verbs.Add( _verbResetTransform );
					_verbs.Add( _verbWarpToCenter );
					_verbs.Add( _verbCenterToWarp );
				}

				UpdateVerbs();

				return _verbs;
			}
		}

		private void OnSaveAs(object sender, EventArgs e)
		{

			ShapeDesigner.SaveShapeAsPicture( this.Shape );
		}


		public virtual void UpdateVerbs()
		{
			if( _verbs == null) return;

			Shape shape = this.Component as Shape;
			if( shape == null) return;
					
			bool visible = !shape.Warp.IsEmpty && !CenterOffset.IsEmpty;
			_verbWarpToCenter.Visible = _verbCenterToWarp.Visible = visible;

			_verbResetTransform.Visible =
				(shape.Rotation != 0 ) ||
				!shape.Scale.IsEmpty   ||
				!shape.Shear.IsEmpty   ||
				!shape.Warp.IsEmpty; 

		}


		static PropertyDescriptor	_propWarp		= TypeDescriptor.GetProperties(typeof( Shape ))["Warp"];
		static PropertyDescriptor	_propScale		= TypeDescriptor.GetProperties(typeof( Shape ))["Scale"];
		static PropertyDescriptor	_propRotation	= TypeDescriptor.GetProperties(typeof( Shape ))["Rotation"];
		static PropertyDescriptor	_propShear		= TypeDescriptor.GetProperties(typeof( Shape ))["Shear"];
		static PropertyDescriptor	_propPos		= TypeDescriptor.GetProperties(typeof( Shape ))["Position"];

		private void OnResetTransform(object sender, EventArgs e)
		{
			try
			{
				Shape shape = this.Component as Shape;

				if( shape != null )
				{
					IDesignerHost host = this.DesignerHost;

				
					//Reset Transformations
					using(DesignerTransaction tx = host.CreateTransaction("Reset Transformations"))
					{
						_propWarp.SetValue( shape, Warp.Empty);
						_propScale.SetValue( shape, Scale.Empty);
						_propShear.SetValue( shape, Shear.Empty);
						_propRotation.SetValue( shape, 0);

						tx.Commit();
					}

				}
			}
			catch( Exception ex )
			{
				FileWatcher.WriteLine( "Failed OnWarpToCenter:" + ex.Message );
				throw ex;
			}

		}

		private PointF CenterOffset
		{
			get
			{
				Shape shape = this.Component as Shape;
				if( shape == null) return PointF.Empty;

				RectangleF bounds	= shape.Bounds;
				PointF p2			= RectangleUtil.CenterPoint( shape.Warp.GetPoints(bounds));
				PointF center		= RectangleUtil.CenterPoint( bounds );

				float px = center.X - p2.X;
				float py = center.Y - p2.Y;

				return new PointF(px, py);
			}
		}

		private void OnWarpToCenter(object sender, EventArgs e)
		{
			try
			{
				Shape shape = this.Component as Shape;

				if( shape != null )
				{
					IDesignerHost host = this.DesignerHost;

					if(CenterOffset.IsEmpty) return;

				
					//WARP TO CENTER
					using(DesignerTransaction tx = host.CreateTransaction("Warp to center"))
					{
						Warp warp = Warp.WarpToCenter( shape.Bounds, shape.Warp.GetPoints(shape.Bounds));
						_propWarp.SetValue( shape, warp);
						tx.Commit();
					}

				}
			}
			catch( Exception ex )
			{
				FileWatcher.WriteLine( "Failed OnWarpToCenter:" + ex.Message );
				throw ex;
			}

		}

		private void OnCenterToWarp(object sender, EventArgs e)
		{
			try
			{
				Shape shape = this.Component as Shape;

				if( shape != null )
				{
					IDesignerHost host = this.DesignerHost;

					RectangleF bounds	= shape.Bounds;
					PointF offset = CenterOffset;
					if( offset.IsEmpty ) return;

					//CENTER TO WARP
					using(DesignerTransaction tx = host.CreateTransaction("Warp center"))
					{
						PointF[] warpedPoints	= shape.Warp.GetPoints(bounds);
						Warp warp = Warp.WarpToCenter( bounds, warpedPoints);
						_propWarp.SetValue( shape, warp);

						bounds.X-=offset.X;
						bounds.Y-=offset.Y;
						shape.LockRotation();
						_propPos.SetValue( shape,Point.Round(bounds.Location) );
						shape.UnlockRotation();
						tx.Commit();
					}

				}
			}
			catch( Exception ex )
			{
				FileWatcher.WriteLine( "Failed OnWarpToCenter:" + ex.Message );
			}

		}


		internal static void SaveShapeAsPicture(Shape shape)
		{
			using(SaveFileDialog saveDlg = new SaveFileDialog())
			{
				string filter = "Bmp files (*.bmp)|*.bmp";
				filter += "|JPEG files (*.jpeg)|*.jpeg";
				filter+="|PNG files (*.png)|*.png";
				filter+="|Gif files (*.gif)|*.gif";//Need to pay royaltees.??? We do not support it then...:)
				filter+="|Tiff files (*.tiff)|*.tiff";
				filter+="|All files (*.*)|*.*";

				string componentName = TypeDescriptor.GetComponentName( shape );
				if( componentName.Length == 0) componentName = "*.bmp";
				else componentName+=".bmp";

				saveDlg.Filter = filter ;
				saveDlg.FilterIndex = 1 ;
				saveDlg.RestoreDirectory = true ;
				saveDlg.FileName = componentName;// "*.bmp";
				
 
				if(saveDlg.ShowDialog() == DialogResult.OK)
				{
					string file = saveDlg.FileName;
					if( file.Length > 0) shape.SaveAsPicture( file );
				}

			}
		}

	}
}
